<xsl:transform
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:myCustDate="urn:custDate">
<xsl:output method="text" omit-xml-declaration="yes" />		
<xsl:template name="DataType">
	<xsl:param name="x"/>
	<xsl:if test="dataType[.='varchar']">string </xsl:if>
	<xsl:if test="dataType[.='char']">string </xsl:if>
	<xsl:if test="dataType[.='nvarchar']">string </xsl:if>
	<xsl:if test="dataType[.='ntext']">string </xsl:if>	
	<xsl:if test="dataType[.='int']">int </xsl:if>
	<xsl:if test="dataType[.='float']">double </xsl:if>
	<xsl:if test="dataType[.='double']">double </xsl:if>
	<xsl:if test="dataType[.='decimal']">Decimal </xsl:if>	
	<xsl:if test="dataType[.='datetime']">DateTime </xsl:if>
	<xsl:if test="dataType[.='bit']">Byte </xsl:if>
	<xsl:if test="dataType[.='bigint']">long </xsl:if>
</xsl:template>
<xsl:template name="DataType11">
	<xsl:param name="x"/>
	<xsl:if test="dataType[.='varchar']">Convert.ToString(</xsl:if>
	<xsl:if test="dataType[.='char']">Convert.ToString(</xsl:if>
	<xsl:if test="dataType[.='nvarchar']">Convert.ToString(</xsl:if>
	<xsl:if test="dataType[.='ntext']">Convert.ToString(</xsl:if>	
	<xsl:if test="dataType[.='int']">Convert.ToInt32(</xsl:if>
	<xsl:if test="dataType[.='float']">Convert.ToSingle(</xsl:if>
	<xsl:if test="dataType[.='double']">Convert.ToDouble(</xsl:if>
	<xsl:if test="dataType[.='decimal']">Convert.ToDecimal(</xsl:if>	
	<xsl:if test="dataType[.='datetime']">Convert.ToDateTime(</xsl:if>
	<xsl:if test="dataType[.='bit']">Convert.ToByte(</xsl:if>
	<xsl:if test="dataType[.='bigint']">Convert.ToInt32(</xsl:if>
</xsl:template>
<xsl:template name="initCap">
  <xsl:param name="x"/>
  <xsl:value-of select="translate(substring($x,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
  <xsl:value-of select="substring($x,2)"/>
</xsl:template>
<xsl:template match="Table">
using System;
using System.Collections;
using TienIch.Facade;
using TienIch.Model;
using System.Data;
namespace TienIch.Business
{
	<xsl:variable name="tableName" select="./name"/>public class <xsl:value-of select="./name"/>BO : BaseBO
	{
		private <xsl:value-of select="./name"/>Facade facade = <xsl:value-of select="./name"/>Facade.Instance;
		protected static <xsl:value-of select="./name"/>BO instance = new <xsl:value-of select="./name"/>BO();

		protected <xsl:value-of select="./name"/>BO()
		{
			this.baseFacade = facade;
		}

		public static <xsl:value-of select="./name"/>BO Instance
		{
			get { return instance; }
		}
	
		public static DataTable KhoiTaoGrid()
        {
			DataTable dt = new DataTable();
			<xsl:for-each select="/Table/column">
			dt.Columns.Add("<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template>", typeof(<xsl:call-template name="DataType"><xsl:with-param name="x" select="./dataType"/></xsl:call-template>));</xsl:for-each>
			return dt;
		}
		/*
		#region Add and update
		{
			GridView view = sender as GridView;
			<xsl:value-of select="./name"/>Model model = new <xsl:value-of select="./name"/>Model();
			<xsl:for-each select="/Table/column">
			model.<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template> =<xsl:call-template name="DataType11">
			
			<xsl:with-param name="x" select="./dataType"/></xsl:call-template>view.GetRowCellValue(e.RowHandle, col<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template>).ToString());</xsl:for-each>
			if (view.GetRowCellValue(e.RowHandle, colID) == DBNull.Value) //add new 
			{
				<xsl:value-of select="./name"/>BO.Instance.Insert(model);
			}
			else //Update
			{
				model.ID = TextUtils.ToInt(view.GetRowCellValue(e.RowHandle, colID).ToString());
				<xsl:value-of select="./name"/>BO.Instance.Update(model);
			}
		}
		#endregion
		*/
		public static void SaveToTable(DataTable dt)
		{
			DataTable dtRemine;
			dtRemine = dt.GetChanges();
			if (dtRemine != null)
			{
				foreach (DataRow item in dtRemine.Rows)
				{
					<xsl:value-of select="./name"/>Model model = new <xsl:value-of select="./name"/>Model();
					<xsl:for-each select="/Table/column">
					model.<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template> =<xsl:call-template name="DataType11"><xsl:with-param name="x" select="./dataType"/></xsl:call-template>item["<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template>"]);</xsl:for-each>
					if (item["ID"] == DBNull.Value) //add new
					{
						<xsl:value-of select="./name"/>BO.Instance.Insert(model);
					}
					else //Update
					{
						model.ID = Convert.ToInt32(item["ID"].ToString());
						<xsl:value-of select="./name"/>BO.Instance.Update(model);
					}
				}
			}
		}
		
		<xsl:for-each select="/Table/column">
		<xsl:if test="isFK[.='true']">
		public ArrayList FindBy<xsl:value-of select="./name"/>(<xsl:call-template name="DataType"><xsl:with-param name="x" select="./dataType"/></xsl:call-template> <xsl:value-of select="./name"/>)
		{
			return facade.FindBy<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template>(<xsl:value-of select="./name"/>);
		}

		</xsl:if>
		<xsl:if test="isUnique[.='true']">
		public <xsl:value-of select="$tableName"/>Model FindBy<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template>(<xsl:call-template name="DataType"><xsl:with-param name="x" select="./dataType"/></xsl:call-template> <xsl:value-of select="./name"/>)
		{
			try
			{
				return facade.FindBy<xsl:call-template name="initCap"><xsl:with-param name="x" select="./name"/></xsl:call-template>(<xsl:value-of select="./name"/>);
			}
			catch (Exception e)
			{
				throw new BOException(e.Message);
			}
		}
		</xsl:if>
	</xsl:for-each>
	}
}
	</xsl:template>
</xsl:transform>		
